package cn.com.nthotline.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import cn.com.nthotline.adapter.EnhancedAdapter;

public class PopupWindowBuilder {
	private final PopupWindow window;
	private Context mContext;
	private LayoutInflater mInflater;

	public PopupWindowBuilder(Context mContext) {
		this.mContext = mContext;
		window = new PopupWindow(mContext);
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		window.setOutsideTouchable(true);
		window.setFocusable(true);
	}

	public PopupWindowBuilder setContentView(int layoutId,int width) {
		View menuView = (View) mInflater.inflate(layoutId, null, false);
		window.setHeight(-2);
		window.setWidth(width);
		window.setContentView(menuView);
		return this;
	}

	public PopupWindowBuilder setListContentView() {
		ListView listView = new ListView(mContext);
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		listView.setLayoutParams(lp);
		window.setContentView(listView);
		return this;
	}

	public PopupWindowBuilder setItems(
			EnhancedAdapter<?> adapter,
			OnItemClickListener listener) {
		ListView listview = (ListView) window.getContentView().findViewById(android.R.id.list);
		listview.setOnItemClickListener(listener);
		listview.setAdapter(adapter);
		return this;
	}
	
	public PopupWindow create() {
		return this.window;
	}
}
